package com.lanxin.qx.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lanxin.qx.entity.QxRole;
import com.lanxin.qx.service.IQxRoleService;
import com.lanxin.qx.service.IQxRoleUserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyq
 * @since 2025-08-01
 */
@Controller
@RequestMapping("/qx/role")
public class QxRoleController {

    private static final Logger logger = Logger.getLogger(QxRoleController.class);

    @Autowired
    private IQxRoleService QxRoleService;

    @Autowired
    private IQxRoleUserService  RoleUserService;

    /**
     * 跳转角色用户设置页面
     */
    @RequestMapping("/toEditRoleEmp")
    public String toEditRoleEmp(Model model, @RequestParam Integer roleId) {
        // 获取角色信息
        QxRole qxRole = QxRoleService.getById(roleId);
        model.addAttribute("qxRole", qxRole);

        // 获取角色已关联的用户ID
        List<Integer> existEmpIds = RoleUserService.getEmpByRoleId(roleId);
        model.addAttribute("existEmpIds", existEmpIds);

        return "qx/EditRoleEmp";
    }

    /**
     * 保存角色用户关联
     */
    @RequestMapping("/saveRoleUser")
    @ResponseBody
    public boolean saveRoleUser(
            @RequestParam Integer roleId,
            @RequestParam(required = false) List<Integer> empIds) { // 此时empIds可正确接收
        return RoleUserService.saveRoleUsers(roleId, empIds);
    }

    /**
     * 分页查询列表
     */
    @RequestMapping("/listRole")
    public String listRole(Model model,
                           @RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "5") Integer pageSize,
                           @RequestParam(required = false) String roleName) {
        // 仅接收参数，调用Service获取分页数据
        IPage<QxRole> pageInfo = QxRoleService.getQxRoleByPage(page, pageSize, roleName);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("searchRoleName", roleName); // 回显搜索条件
        return "qx/listRole";
    }

    /**
     * 删除操作
     */
    @RequestMapping("/deleteQxRole")
    @ResponseBody
    public boolean deleteQxRole(@RequestParam Integer oid) {
        // 仅传参给Service，返回结果
        boolean result = QxRoleService.removeById(oid);
        return result;
    }

    /**
     * 跳转新增页面
     */
    @RequestMapping("/toAddQxRole")
    public String toAddQxRole() {
        return "qx/AddQxRole";
    }

    /**
     * 执行新增
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @RequestMapping("/addQxRole")
    public String addQxRole(QxRole QxRole) {
        // 接收实体参数，调用Service保存
        boolean result = QxRoleService.save(QxRole);
        return "redirect:/qx/role/listRole"; // 重定向到列表页
    }

    /**
     * 跳转修改页面
     */
    @RequestMapping("/toUpdateQxRole")
    public String toUpdateQxRole(Model model, @RequestParam Integer oid) {
        QxRole qxRole = QxRoleService.getById(oid);
        model.addAttribute("qxRole", qxRole); // 页面使用${qxRole}获取数据
        return "qx/UpdateQxRole";
    }

    /**
     * 执行修改
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @RequestMapping("/updateQxRole")
    public String updateQxRole(QxRole QxRole) {
        // 接收实体参数，调用Service更新
        boolean result = QxRoleService.updateById(QxRole);
        return "redirect:/qx/role/listRole"; // 重定向到列表页
    }

    /**
     * 校验类型名称是否已经存在
     */
    @RequestMapping("/checkQxRoleName")
    @ResponseBody
    public Map<String, Boolean> checkQxRoleName(
            @RequestParam String roleName,
            @RequestParam(required = false) Integer oid) {
        // 传递参数给Service校验，返回结果
        boolean exists = QxRoleService.checkRoleName(roleName, oid);
        Map<String, Boolean> result = new HashMap<>();
        result.put("valid", !exists); // valid为true表示可用
        return result;
    }

}
